package com.tb.foryou;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ToxicBakery.viewpager.transforms.DepthPageTransformer;

import java.util.ArrayList;
import java.util.List;


public class MainView extends ViewPager{

    private PagerAdapter adapter;
    private List<View> viewList;
    private Context context;

    //时间未到
    private static final int DAY_BEFORE = -1;
    //时间已过
    private static final int DAY_AFTER = -2;

    //初始化mainView
    public void init(String[] books, int dayToStart){

        //设置短语集
        //起始页
        viewList.add(LayoutInflater.from(context).inflate(R.layout.main_view_head,null));
        //正常页
        for (int i = 0 ; i < dayToStart + 1; i++){
            if(i == books.length){
                viewList.add(LayoutInflater.from(context).inflate(R.layout.main_view_last,null));
                break;
            }
            View view = LayoutInflater.from(context).inflate(R.layout.main_view,null);
            ((TextView)view.findViewById(R.id.main_book)).setText(books[i]);
            ((TextView)view.findViewById(R.id.day_counter)).setText("" + (i + 1));
            viewList.add(view);
        }

        this.adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));

            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);

            }
        };
        this.setAdapter(adapter);
        this.setCurrentItem(viewList.size()-1);
        this.setPageTransformer(true, new DepthPageTransformer());
        addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float onPageScrolled, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    //跳转页面
    public void goHeadPage(){
        this.setCurrentItem(0);
    }
    public void goNowPage(){
        this.setCurrentItem(viewList.size() - 1);
    }


    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.viewList = new ArrayList<>();
        this.context = context;
    }
}
